package com.ty.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.annotation.Auth;
import com.ty.entity.BookTypeEntity;
import com.ty.resp.CommonResp;
import com.ty.service.IBookTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2024/1/20 17:13
 */
@RestController
@RequestMapping("/bookType")
public class BookTypeController {
    @Resource
    private IBookTypeService bookTypeService;

    @GetMapping("/getBookType")
    public CommonResp getBookType() {
        List<BookTypeEntity> typeAll = bookTypeService.list();
        CommonResp resp = new CommonResp<>();
        resp.setContent(typeAll);
        return resp;
    }
    @GetMapping("/getBookTypes")
    public CommonResp getBookTypes(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                   @RequestParam(value = "size",defaultValue = "5")Integer size) {
        Page page1 = new Page<>(page, size);
        List<BookTypeEntity> typeAll = bookTypeService.list(page1);
        CommonResp resp = new CommonResp<>();
        resp.setContent(typeAll);
        resp.setTotal(page1.getTotal());
        return resp;
    }
    @Auth(roles = "admin")
    @PostMapping("/addBookType")
    public CommonResp addBookType (BookTypeEntity periodicalType){
        CommonResp resp = new CommonResp();
        boolean save = bookTypeService.save(periodicalType);
        if (!save) {
            resp.setMessage("新增期刊类型失败");
        }
        resp.setSuccess(save);
        return resp;
    }
    @Auth(roles = "admin")
    @PostMapping("/deleteBookType")
    public CommonResp deleteBookType (BookTypeEntity periodicalType){
        CommonResp resp = new CommonResp();
        boolean remove = bookTypeService.removeById(periodicalType);
        if (!remove) {
            resp.setMessage("删除期刊类型失败");
        }
        resp.setSuccess(remove);
        return resp;
    }
    @Auth(roles = "admin")
    @PostMapping("/updateBookType")
    public CommonResp updateBookType (BookTypeEntity periodicalType){
        CommonResp resp = new CommonResp();
        boolean remove = bookTypeService.updateById(periodicalType);
        if (!remove) {
            resp.setMessage("修改类型失败");
        }
        resp.setSuccess(remove);
        return resp;
    }
}
